package com.ruoyi.leave.repository;


import com.ruoyi.leave.entity.LeaveInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 请假 仓库
 *
 * @author zhangxingju
 * @date 2025/02/15
 */
public interface LeaveInfoRepository extends JpaRepository<LeaveInfo, String> {

    List<LeaveInfo> findAllByCreateByOrderByCreateTimeDesc(String createBy);


    @Query(value = """
            SELECT li.*, ht.id as taskId
            FROM leave_info li
            INNER JOIN dk_runtime_task ht ON li.id = ht.business_id
            INNER JOIN dk_activity ac ON ht.activity_id = ac.id
            WHERE ac.type='activity' and (ac.departments LIKE CONCAT('%', :deptId, '%')
               OR ac.approvers LIKE CONCAT('%', :userId, '%'))
               AND ht.status = :status
            """, nativeQuery = true)
    List<Object[]> findListByUserIdOrDeptIdAndStatus(
            @Param("userId") String userId,
            @Param("deptId") String deptId,
            @Param("status") int status
    );

}